﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.DataModel
{
	public class ProInfoOperate
	{
		public ProInfoOperate()
		{
		}

		public ProInfoOperate(string info)
		{
			this.xmlDoc = XMLOperating.LoadXml(ProInfoOperate.proInfoFile);
			this.listItem = XMLOperating.GetAllElements(this.xmlDoc, "ProjectInfo");
		}

		public List<HYProjectInfo> GetAllInfo()
		{
			List<HYProjectInfo> list = new List<HYProjectInfo>();
			foreach (XmlElement xmlElement in this.listItem)
			{
				HYProjectInfo hyprojectInfo = new HYProjectInfo();
				hyprojectInfo.TypeName = xmlElement.GetAttribute("type");
				hyprojectInfo.ParamName = xmlElement.GetAttribute("name");
				string attribute = xmlElement.GetAttribute("value");
				if (string.IsNullOrEmpty(attribute))
				{
					hyprojectInfo.ParamValue = hyprojectInfo.ParamName;
				}
				else
				{
					hyprojectInfo.ParamValue = attribute;
				}
				list.Add(hyprojectInfo);
			}
			return list;
		}

		public void SaveFile()
		{
			XMLOperating.SaveXml(this.xmlDoc, ProInfoOperate.proInfoFile);
		}

		public void ModifyParamValue(XmlElement xmlElem, string paramValue)
		{
			xmlElem.SetAttribute("value", paramValue);
		}

		public void DeleteByParamName(string typeName, string paramName)
		{
			XmlElement oldChild = null;
			foreach (XmlElement xmlElement in this.listItem)
			{
				string attribute = xmlElement.GetAttribute("type");
				string attribute2 = xmlElement.GetAttribute("name");
				if (attribute == typeName && attribute2 == paramName)
				{
					oldChild = xmlElement;
					break;
				}
			}
			this.xmlDoc.SelectSingleNode("ProjectInfo").RemoveChild(oldChild);
		}

		public void DeleteByTypeName(string typeName)
		{
			foreach (XmlElement xmlElement in this.listItem)
			{
				string attribute = xmlElement.GetAttribute("type");
				if (attribute == typeName)
				{
					this.xmlDoc.SelectSingleNode("ProjectInfo").RemoveChild(xmlElement);
				}
			}
		}

		public bool AddProInfo(HYProjectInfo hyProInfo)
		{
			XmlElement xmlElement = this.xmlDoc.CreateElement("Item");
			xmlElement.SetAttribute("type", hyProInfo.TypeName);
			xmlElement.SetAttribute("name", hyProInfo.ParamName);
			xmlElement.SetAttribute("value", hyProInfo.ParamValue);
			this.xmlDoc.SelectSingleNode("ProjectInfo").AppendChild(xmlElement);
			return true;
		}

		public bool UpdateInfo(HYProjectInfo hyProInfo)
		{
			foreach (XmlElement xmlElement in this.listItem)
			{
				string attribute = xmlElement.GetAttribute("type");
				string attribute2 = xmlElement.GetAttribute("name");
				if (attribute == hyProInfo.TypeName && attribute2 == hyProInfo.ParamName)
				{
					xmlElement.SetAttribute("value", hyProInfo.ParamValue);
					break;
				}
			}
			return true;
		}

		public string SetProjectShareParam(UIApplication uiApp)
		{
			DefinitionFile definitionFile = YJKRevitTools.OpenShareParamFile(uiApp, this.sharePFPath);
			if (definitionFile == null)
			{
				return "共享参数文件不存在！";
			}
			CategorySet bdcategorySet = YJKRevitTools.GetBDCategorySet(uiApp, BuiltInCategory.OST_ProjectInformation);
			ElementBinding elementBinding = YJKRevitTools.CreatElemBinding(uiApp, bdcategorySet, "instance");
			InstanceBinding elemBinding = elementBinding as InstanceBinding;
			List<string> list = new List<string>();
			foreach (XmlElement xmlElement in this.listItem)
			{
				list.Add(xmlElement.GetAttribute("name"));
			}
			List<string> proInfoParamName = this.GetProInfoParamName(uiApp);
			List<string> creatParamName = this.GetCreatParamName(proInfoParamName, list);
            return YJKRevitTools.SetShareParameter(uiApp, definitionFile, "项目信息", creatParamName, elemBinding, (BuiltInParameterGroup)(-5000123)).ToString();
		}

		private List<string> GetProInfoParamName(UIApplication uiApp)
		{
			List<string> list = new List<string>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiApp.ActiveUIDocument.Document);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_ProjectInformation);
			foreach (object obj in filteredElementCollector.First<Element>().Parameters)
			{
				Parameter parameter = (Parameter)obj;
				list.Add(parameter.Definition.Name);
			}
			return list;
		}

		public void SetProjectParam(UIApplication uiApp, List<HYProjectInfo> listProInfo)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiApp.ActiveUIDocument.Document);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_ProjectInformation);
			foreach (object obj in filteredElementCollector.First<Element>().Parameters)
			{
				Parameter parameter = (Parameter)obj;
				foreach (HYProjectInfo hyprojectInfo in listProInfo)
				{
					if (parameter.Definition.Name == hyprojectInfo.ParamName)
					{
						parameter.Set(hyprojectInfo.ParamValue);
					}
				}
			}
		}

		private List<string> GetCreatParamName(List<string> listProParamName, List<string> listXMLName)
		{
			List<string> list = new List<string>();
			foreach (string item in listXMLName)
			{
				if (!listProParamName.Contains(item))
				{
					list.Add(item);
				}
			}
			return list;
		}

		private static string proInfoFile = Product.InstallLocation + "\\Data\\\\ProjectInfoName.xml";

		private string sharePFPath = Product.InstallLocation + "\\Data\\\\ProjectInfoSet.txt";

		public List<string> listTypeName;

		private XmlDocument xmlDoc;

		private List<XmlElement> listItem;
	}
}
